import keyring
from imbox import Imbox
import datetime

password = keyring.get_password('qqmail', '1769699920')
with Imbox('imap.qq.com', '1769699920@qq.com', password, ssl=True) as imbox:
    # 获取全部邮件
    # all_inbox_messages = imbox.messages()
    unread_inbox_messages = imbox.messages(unread=True)  # 未读邮件
    # read_inbox_messages = imbox.messages(unread=False)  # 已读邮件
    # flagged_inbox_messages = imbox.messages(flagged=True)  # 红旗标记邮件
    # 指定日期
    # inbox_message_before = imbox.messages(date__lt=datetime.date(2021, 1, 18))
    # inbox_message_after = imbox.messages(date__gt=datetime.date(2021, 1, 18))
    # inbox_message_on_date = imbox.messages(date__on=datetime.date(2021, 5, 24))
    for uid, message in unread_inbox_messages:
        print(message.subject)  # 邮件主题
        print(message.body['plain'])  # 邮件文本格式正文
        print(message.sent_from)
        print(message.date)
        unread_inbox_messages
        # 把邮箱提取出来
        email = message.sent_from[0]['email']
        if email == '1769699920@qq.com':
            imbox.mark_seen(uid)
            # 标记已读 imbox.mark_seen(uid)
            # 删除邮件 imbox.delete(uid)
